package org.ihelpuoo.parser.sql;

import org.ihelpuoo.lang.LoaderException;

public class FieldType
{
	private static final int	TYPE_INT		= 1;
	private static final int	TYPE_FLOAT		= 2;
	private static final int	TYPE_BOOLEAN	= 3;
	private static final int	TYPE_BLOB		= 4;
	private static final int	TYPE_TEXT		= 5;
	private static final int	TYPE_DATE		= 6;
	private static final int	TYPE_TIME		= 7;
	private static final int	TYPE_DATE_TIME	= 8;
	private static final int	TYPE_TIMESTAMP	= 9;
	private static final int	TYPE_BINARY		= 10;

	private String				name;
	private int					precision;
	private int					size;
	private boolean				isUnsigned;
	private String				_s;
	private int					type;

	protected FieldType(String s) throws LoaderException
	{
		_s = s;
		int pos = s.indexOf('(');
		if (pos == -1)
			name = s.toUpperCase();
		else
		{
			name = s.substring(0,pos).toUpperCase();
			pos++;
			int pe = s.lastIndexOf(')');
			if (-1 == pe)
				throw new LoaderException("Error field type \"" + s + "\"");
			int seg = s.indexOf(',',pos);
			if (-1 == seg)
			{
				try
				{
					size = Integer.parseInt(s.substring(pos,pe));
					precision = 0;
				}
				catch (Exception e)
				{
					throw new LoaderException("Error field type \"" + s + "\"");
				}
			}
			else
			{
				try
				{
					size = Integer.parseInt(s.substring(pos,seg++));
					precision = Integer.parseInt(s.substring(seg,pe));
				}
				catch (Exception e)
				{
					throw new LoaderException("Error field type \"" + s + "\"");
				}
			}
		}
		retrieveType();
	}

	private void retrieveType()
	{
		if (name.indexOf("INT") >= 0)
			this.type = TYPE_INT;
		else if ("FLOAT".equals(name))
			this.type = TYPE_FLOAT;
		else if ("DOUBLE".equals(name))
			this.type = TYPE_FLOAT;
		else if ("DECIMAL".equals(name))
			this.type = TYPE_FLOAT;
		else if (name.indexOf("CHAR") >= 0)
			this.type = TYPE_TEXT;
		else if (name.indexOf("TEXT") >= 0)
			this.type = TYPE_TEXT;
		else if ("BOOLEAN".equals(name))
			this.type = TYPE_BOOLEAN;
		else if ("DATE".equals(name))
			this.type = TYPE_DATE;
		else if ("TIME".equals(name))
			this.type = TYPE_TIME;
		else if ("DATETIME".equals(name))
			this.type = TYPE_DATE_TIME;
		else if ("TIMESTAMP".equals(name))
			this.type = TYPE_TIMESTAMP;
		else if ("BIT".equals(name))
			this.type = TYPE_BOOLEAN;
		else if (name.indexOf("BLOB") >= 0)
			this.type = TYPE_BLOB;
		else if (name.indexOf("BINARY") >= 0)
			this.type = TYPE_BINARY;
	}

	public boolean isTime()
	{
		return type == TYPE_TIME;
	}

	public boolean isDate()
	{
		return type == TYPE_DATE;
	}

	public boolean isTimeStamp()
	{
		return type == TYPE_TIMESTAMP;
	}

	public boolean isNumerice()
	{
		return type == TYPE_FLOAT || type == TYPE_INT;
	}

	public boolean isText()
	{
		return type == TYPE_TEXT;
	}

	public boolean isDateTime()
	{
		return type == TYPE_DATE_TIME;
	}

	public boolean isBoolean()
	{
		return type == TYPE_BOOLEAN;
	}

	public boolean isBlob()
	{
		return type == TYPE_BLOB;
	}

	public boolean isBinary()
	{
		return type == TYPE_BINARY;
	}

	@Override
	public String toString()
	{
		if (isUnsigned)
			return _s;
		else
			return _s + " unsigned";
	}

	public boolean isUnsigned()
	{
		return isUnsigned;
	}

	public void setUnsigned(boolean isUnsigned)
	{
		this.isUnsigned = isUnsigned;
	}

	public String getName()
	{
		return name;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	public int getPrecision()
	{
		return precision;
	}

	public void setPrecision(int precision)
	{
		this.precision = precision;
	}

	public int getSize()
	{
		return size;
	}

	public void setSize(int size)
	{
		this.size = size;
	}

}
